package where2go.sctu.com.zhiyou.activity;

import android.app.DownloadManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.entity.NewUser;
import where2go.sctu.com.zhiyou.entity.WalltData;
import where2go.sctu.com.zhiyou.utils.HeaderStringRequest;
import where2go.sctu.com.zhiyou.utils.ShareUtils;

/**
 * Created by lenovo on 2018/5/23.
 */

public class MyWalletActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView imageView;

    private TextView money;
    private LinearLayout walletMoneyLinear;
    private Button btnRecharge;
    private EditText etMoney;
    private Button btnStart;

    private String url="http://47.104.80.155/youyiyou/users/details/addBalance?money=";
    WalltData walltData=new WalltData();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_wallet);
        initView();
        imageView = (ImageView) findViewById(R.id.wallet);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
    private void initView() {
        money=(TextView)findViewById(R.id.money);
        walletMoneyLinear=(LinearLayout) findViewById(R.id.wallet_recharge);
        btnRecharge=(Button) findViewById(R.id.btn_recharge);
        btnRecharge.setOnClickListener(this);
        btnStart=(Button)findViewById(R.id.btn_start);
        btnStart.setOnClickListener(this);
        etMoney=(EditText)findViewById(R.id.et_money);


    }

    private void getMoney(String url) {

        HeaderStringRequest headerStringRequest=new HeaderStringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
                Type type = new TypeToken<WalltData>() {
                }.getType();
                walltData=gson.fromJson(response,type);
                int error=walltData.getError();
                if (error==0){
                    Toast.makeText(MyWalletActivity.this,"充值成功",Toast.LENGTH_SHORT).show();
                    money.setText(etMoney.getText());
                }else {
                    Toast.makeText(MyWalletActivity.this,"充值失败",Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("authorization", ShareUtils.getString(MyWalletActivity.this, "token", null));
                return map;
            }
        };
        MyApplication.getHttpQueues().add(headerStringRequest);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_recharge:
                walletMoneyLinear.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_start:
                String money= String.valueOf(etMoney.getText());
                getMoney(url+money);
                walletMoneyLinear.setVisibility(View.GONE);

                break;
        }
    }
}
